package services

import (
	"purchase-mall/models"
	"time"
)

func BubbleSortTokens(tokens []*models.TokenList) {
	for i := 0; i <= len(tokens)-1; i++ {
		for j := i + 1; j <= len(tokens)-1; j++ {

			if tokens[i].UseTotal < tokens[j].UseTotal {
				tokens[i], tokens[j] = tokens[j], tokens[i]
			}
		}
	}
}

func BubbleSortTop20s(top20s []*models.Top20) {
	for i := 0; i <= len(top20s)-1; i++ {
		for j := i + 1; j <= len(top20s)-1; j++ {

			if top20s[i].RequestNum < top20s[j].RequestNum {
				top20s[i], top20s[j] = top20s[j], top20s[i]
			}
		}
	}
}

func BubbleSortIps(ips []*models.Ip) {
	for i := 0; i <= len(ips)-1; i++ {
		for j := i + 1; j <= len(ips)-1; j++ {
			if ips[i].UseTotal < ips[j].UseTotal {
				ips[i], ips[j] = ips[j], ips[i]
			}
		}
	}
}

func BubbleSortPeak(peakValues []*models.PeakValueInfo) {
	for i := 0; i <= len(peakValues)-1; i++ {
		for j := i + 1; j <= len(peakValues)-1; j++ {
			ti, _ := time.ParseInLocation("2006-01-02", peakValues[i].Time, time.Local)
			tj, _ := time.ParseInLocation("2006-01-02", peakValues[j].Time, time.Local)
			if ti.Unix() > tj.Unix() {
				peakValues[i], peakValues[j] = peakValues[j], peakValues[i]
			}
		}
	}
}

func BubbleAccessTotal(accessTotals []*models.AccessTotalInfo) {
	for i := 0; i <= len(accessTotals)-1; i++ {
		for j := i + 1; j <= len(accessTotals)-1; j++ {
			ti, _ := time.ParseInLocation("2006-01-02", accessTotals[i].Time, time.Local)
			tj, _ := time.ParseInLocation("2006-01-02", accessTotals[j].Time, time.Local)
			if ti.Unix() > tj.Unix() {
				accessTotals[i], accessTotals[j] = accessTotals[j], accessTotals[i]
			}
		}
	}
}
